<template>
  <div id="tagCategoryTopSearch">
    <h1 class="con-right-title">
      标签分类管理
    </h1>
    <div class="search-box">
      <el-form
        ref="ruleForm"
        :inline="true"
        :model="formInline"
        :rules="rules"
        class="demo-form-inline"
      >
        <el-form-item label="创建时间" prop="createTime">
          <el-date-picker
            v-model="formInline.createTime"
            type="datetimerange"
            range-separator="至"
            start-placeholder="开始时间"
            end-placeholder="结束时间"
            value-format="yyyy-MM-dd HH:mm:ss"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="分类搜索" prop="text">
          <el-input
            v-model="content"
            :type="contentType"
            :placeholder="contentPH"
            clearable
          >
            <el-select
              slot="prepend"
              v-model="formInline.tagSelectType"
              class="searchCategory"
              placeholder="请选择搜索方式"
              clearable
            >
              <el-option
                v-for="item in searchList"
                :key="item.dictLabel"
                :label="item.dictLabel"
                :value="item.dictValue"
              ></el-option>
            </el-select>
          </el-input>
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-select
            v-model="formInline.status"
            placeholder="请选择状态"
            clearable
          >
            <el-option
              v-for="item in statusList"
              :key="item.dictLabel"
              :label="item.dictLabel"
              :value="item.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item class="s-btn">
          <el-button
            type="success"
            icon="el-icon-search"
            @click="toSearch('ruleForm')"
          >
            查询
          </el-button>
          <el-button type="success" plain @click="reset">
            重置
          </el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import { sysNormalDisable } from '@utils/dictionariesFunc'
export default {
  name: 'TagCategoryTopSearch',
  msg: ' 标签分类管理-顶部搜索',
  props: {
    formInline: {
      type: Object,
      default: function() {
        return {
          tagSelectType: '', // 搜索方式
          status: '', // 状态（启用:true/停用:false）默认为启用
          startTime: '', // 开始时间
          endTime: '', // 结束时间
          id: '', // 标签分类ID
          tagCategoryName: '', // 标签分类名称
          createTime: []
        }
      }
    }
  },
  data() {
    return {
      content: '', //输入内容
      contentType: '', //输入内容格式
      contentPH: '请输入', //输入内容提示
      statusList: [
        // 用户状态
        { dictLabel: '全部', dictValue: '' }
      ],
      searchList: [], //分类搜索
      rules: {
        status: {},
        createTime: { type: 'array' }
      }
    }
  },
  watch: {
    'formInline.createTime': {
      handler(newVal, oldVal) {
        if (newVal && newVal.length > 1) {
          this.formInline.startTime = newVal[0]
          this.formInline.endTime = newVal[1]
        } else {
          this.formInline.startTime = ''
          this.formInline.endTime = ''
        }
      },
      deep: true
    },
    'formInline.tagSelectType': {
      handler(newVal, oldVal) {
        this.content = ''
        this.contentType = 'text'
        this.contentPH = '请输入'
        if (newVal === 'id') {
          this.contentType = 'number'
          this.contentPH = '请输入数字'
        }
      },
      deep: true
    }
  },
  mounted() {
    this.getStatusList()
  },
  methods: {
    /* 搜索 */
    toSearch(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.formInline[this.formInline.tagSelectType] = this.content
          this.$emit('toSearch')
        } else {
          return false
        }
      })
    },
    /* 获取数据状态 */
    async getStatusList() {
      // 用户状态
      let data1 = await sysNormalDisable('tag_status')
      this.statusList = this.statusList.concat(data1)
      // 分类搜索
      let data2 = await sysNormalDisable('tag_category_type')
      this.searchList = this.searchList.concat(
        { dictLabel: '-请选择搜索方式-', dictValue: '', dictType: '' },
        data2
      )
      //设置默认数据
      this.setDefaultValue()
    },
    /*设置默认数据*/
    setDefaultValue() {
      this.formInline.status = this.statusList[0].dictValue
    },
    /* 重置 */
    reset() {
      this.$refs['ruleForm'].resetFields()
      this.formInline.tagSelectType = ''
      this.formInline.tagCategoryName = ''
      this.formInline.id = ''
      this.content = ''
      //设置默认数据
      this.setDefaultValue()
    }
  }
}
</script>

<style lang="less">
#tagCategoryTopSearch {
  input::-webkit-outer-spin-button,
  input::-webkit-inner-spin-button {
    -webkit-appearance: none;
  }
  input[type='number'] {
    -moz-appearance: textfield;
  }
  .searchCategory {
    .el-input.el-input--suffix {
      width: 155px !important;
    }
  }
}
</style>
